package cn.comon.app.scan.service.analysis.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import cn.comon.app.scan.component.SysConfigBean;
import cn.comon.app.scan.entity.AsCheckresult;
import cn.comon.app.scan.entity.AsReportinfo;
import cn.comon.app.scan.service.AsCheckresultService;
import cn.comon.app.scan.service.AsReportinfoService;
import cn.comon.app.scan.service.analysis.IAnalysisScanResult;
import cn.comon.app.scan.util.UtilTools;
@Service("qqAnalysisScanResultImpl")
@Transactional
public class TencentAnalysisScanResultImpl implements IAnalysisScanResult {
    @Autowired
    private AsReportinfoService asReportinfoServiceImpl;;
    @Autowired
    private AsCheckresultService asCheckresultServiceImpl;
    
    private JSONObject saveScanResult(AsCheckresult asCheckresult, AsReportinfo asReportinfo) {
        //检索AsCheckresult表，确认该条记录是否曾经被同一家检测方检测过
        //记录不存在，插入新记录
        //记录存在，更新检测信息
        AsCheckresult asCheckresultTMP =
            asCheckresultServiceImpl.findByRidAndResultIndex(asCheckresult);
        if (asCheckresultTMP == null) {
            asCheckresultServiceImpl.addObj(asCheckresult);
        } else {
            asCheckresultServiceImpl.updateCheckresult(asCheckresult);
        }
        
        asReportinfoServiceImpl.updateReportinfo(asReportinfo);
        return new JSONObject();
    }
    
	public JSONObject checkResult(JSONObject resultData) {

        JSONArray jsonArray1 = resultData.getJSONArray("resultlist");
        for (int i = 0 ;i < jsonArray1.size(); i++) {
            JSONObject jsonObjTMP = jsonArray1.getJSONObject(i);
            AsCheckresult asCheckresult = new AsCheckresult();
            //通过返回的sid,检索reportinfo表里对应的记录
            AsReportinfo asReportinfo = asReportinfoServiceImpl.findByID(jsonObjTMP.getInt("sid"));
            asCheckresult.setRid(asReportinfo.getId());
            asCheckresult.setPackageUrlHash(asReportinfo.getPackageUrlHash());
            //腾讯
            asCheckresult.setResultIndex(1);
            if (jsonObjTMP.containsKey("safetype")) {
                switch (jsonObjTMP.getInt("safetype")) {
                case 1:
                    asCheckresult.setSafeType(1);
                     break;
                case 2:
                    asCheckresult.setSafeType(2);
                    break;
                case 3:
                    asCheckresult.setSafeType(3);
                    break;
                case 4:
                    asCheckresult.setSafeType(4);
                    break;
                }
            }

            if(jsonObjTMP.containsKey("virusname")) {
                asCheckresult.setVirusName(jsonObjTMP.getString("virusname"));
            }
            if(jsonObjTMP.containsKey("virusdesc")) {
                asCheckresult.setVirusDesc(jsonObjTMP.getString("virusdesc"));
            }
            asCheckresult.setResultDescr("");

            String adinfo = "";
            String notifybar = "";
            if (jsonObjTMP.containsKey("notifybar")) {
                if (jsonObjTMP.getInt("notifybar") == -1) {
                    notifybar = "通知栏广告软件(不确定)";
                } else if (jsonObjTMP.getInt("notifybar") == 0) {
                    notifybar = "非通知栏广告软件";
                } else if (jsonObjTMP.getInt("notifybar") == 1) {
                    notifybar = "通知栏广告软件";
                }
            }
            String integralwall = "";
            if (jsonObjTMP.containsKey("integralwall")) {
                if (jsonObjTMP.getInt("integralwall") == -1) {
                    integralwall = "积分墙广告软件(不确定)";
                } else if (jsonObjTMP.getInt("integralwall") == 0) {
                    integralwall = "非积分墙广告软件";
                } else if (jsonObjTMP.getInt("integralwall") == 1) {
                    integralwall = "积分墙广告软件";
                }
            }
            adinfo = notifybar + ";" + integralwall;
            asCheckresult.setAdinfo(UtilTools.deleteComma(adinfo));
            asCheckresult.setScore(0);
            asCheckresult.setSize(0);
            asCheckresult.setPlatform("");
            if (jsonObjTMP.containsKey("official")) {
                asCheckresult.setOfficial(jsonObjTMP.getInt("official"));
            }
            
            asCheckresult.setCreateTime(UtilTools.getTimestamp(0));
            asCheckresult.setLastUpdateTime(UtilTools.getTimestamp(0));

            //AsReportinfo表更新（checkCount，expired，lastUpdateTime）
            AsReportinfo asReportinfoTMP = new AsReportinfo();
            asReportinfoTMP.setId(asReportinfo.getId());
            //安全基本不确定，过期时间统一延后15天
            int expired = 0;
            switch (jsonObjTMP.getInt("safetype")) {
            case 1:
                //检测结果为安全，但是举报次数又多时，将缩短再次检测时间间隔
                expired = Integer.parseInt(SysConfigBean.getProperty("result.safetype.safe")); //推后180天再扫描
                break;
            default:
                expired = Integer.parseInt(SysConfigBean.getProperty("result.safetype.dangerous"));
                break;
            }
            //Expired过期时间，SQL语句中计算更新
            asReportinfoTMP.setExpired(expired);
            asReportinfoTMP.setCheckTime(UtilTools.getTimestamp(0));
            
            saveScanResult(asCheckresult, asReportinfoTMP);
        }
        JSONObject result = new JSONObject();
        result.put("result", "ok");
    	result.put("reason", "");
    
		return result;
	}
}
